package com.twang.stock.controller.old;

import com.twang.stock.entity.*;
import com.twang.stock.mapper.CompanyCodeInfoMapper;
import com.twang.stock.mapper.StockTagMapper;
import com.twang.stock.common.HotTagCommon;
import com.twang.stock.service.jinyan.HolidayService;
import com.twang.stock.service.jinyan.HotTagService;
import com.twang.stock.service.jinyan.StockInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/jingxuan")
@Slf4j
public class JingXuanController {

    @Autowired
    private HotTagService hotTagService;

    @Resource
    private CompanyCodeInfoMapper companyCodeInfoMapper;

    @Autowired
    private HolidayService holidayService;
    @Resource
    private StockTagMapper stockTagMapper;
    @Autowired
    private StockInfoService sockInfoService;

    /**
     * 情绪指数
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {
        ModelAndView modelAndView = new ModelAndView();
        HotTag hotTag = hotTagService.createHotTagByName(HotTagCommon.getHotTagBynameAnddate(HotTagCommon.HOT_TAG_JINGXUAN, holidayService.getDayByNum精研(0)));
        StockTag param=new StockTag();
        param.setTagId(hotTag.getId());
       List<StockTag>  stockTagList=stockTagMapper.select(param);
       if(CollectionUtils.isNotEmpty(stockTagList)){
           List<CompanyCodeInfoEx> companyCodeInfoList= Lists.newArrayList();
           for(StockTag stockTag:stockTagList) {
               CompanyCodeInfoEx companyCodeInfoEx=new CompanyCodeInfoEx();
               CompanyCodeInfo companyCodeInfo=companyCodeInfoMapper.selectByPrimaryKey(stockTag.getStockId());
               BeanUtils.copyProperties(companyCodeInfo,companyCodeInfoEx);
                StockInfo stockInfo= sockInfoService.getLastData(companyCodeInfo.getCompanyCode());
                if(null==stockInfo){
                    continue;
                }
                //流入资金转换
               BigDecimal moneyIn=companyCodeInfoEx.getTotalMoneyIn();
               companyCodeInfoEx.setTotalMoneyIn(null==moneyIn?BigDecimal.ZERO:moneyIn.divide(new BigDecimal(10000),2,BigDecimal.ROUND_HALF_UP));
               companyCodeInfoEx.setZhangDie(stockInfo.getNotNullZhangDie());
               companyCodeInfoEx.setVol(stockInfo.getVolMay1());
               companyCodeInfoEx.setVolMay5(stockInfo.getVolMay5());
               companyCodeInfoEx.setPrice(stockInfo.getPrice());
               companyCodeInfoEx.setPriceMay5(stockInfo.getPriceMay5());
               companyCodeInfoList.add(companyCodeInfoEx);
           }
           modelAndView.addObject("resultList", companyCodeInfoList);
       }
        modelAndView.setViewName("jingxuan");
        return modelAndView;
    }
    /**
     * 情绪指数
     * @return
     */
    @GetMapping(value = "/updateHotTag")
    public ModelAndView updateHotTag() {
        hotTagService.getHotTemperatureName();
        return  query();
    }

}
